package com.sttk.fastMark.camera.dialog;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.sttk.fastMark.camera.R;
import com.sttk.fastMark.camera.basic.dialog.BaseSmartDialog;
import com.sttk.fastMark.camera.databinding.DialogNumberBinding;

public class NumberDialog extends BaseSmartDialog<DialogNumberBinding> implements View.OnClickListener {

    private String text;

    public NumberDialog(@NonNull Context context) {
        super(context);
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_number;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        if (!TextUtils.isEmpty(text)) {
            String[] parts = text.split("-");
            mDataBinding.etNumber1.setText(parts[0]);
            mDataBinding.etNumber2.setText(parts[1]);
            mDataBinding.etNumber3.setText(parts[2]);
        }

        mDataBinding.etNumber1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String str = charSequence.toString();
                if (TextUtils.isEmpty(str)) {
                    mDataBinding.ivNumber1Clear.setVisibility(View.GONE);
                } else {
                    mDataBinding.ivNumber1Clear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        mDataBinding.etNumber2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String str = charSequence.toString();
                if (TextUtils.isEmpty(str)) {
                    mDataBinding.ivNumber2Clear.setVisibility(View.GONE);
                } else {
                    mDataBinding.ivNumber2Clear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        mDataBinding.etNumber3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String str = charSequence.toString();
                if (TextUtils.isEmpty(str)) {
                    mDataBinding.ivNumber3Clear.setVisibility(View.GONE);
                } else {
                    mDataBinding.ivNumber3Clear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            String number1 = mDataBinding.etNumber1.getText().toString();
            String number2 = mDataBinding.etNumber1.getText().toString();
            String number3 = mDataBinding.etNumber1.getText().toString();
            if (!TextUtils.isEmpty(number1) && !TextUtils.isEmpty(number2) && !TextUtils.isEmpty(number3)) {
                dismiss();
                String number = number1 + "-" + number2 + "-" + number3;
                if (!text.equals(number)) {
                    if (listener != null) {
                        listener.clickConfirm(number);
                    }
                }
            } else {
                ToastUtils.showShort(R.string.please_input_custom_text);
            }
        } else if (id == R.id.ivNumber1Clear) {
            mDataBinding.etNumber1.setText("");
        } else if (id == R.id.ivNumber2Clear) {
            mDataBinding.etNumber2.setText("");
        } else if (id == R.id.ivNumber3Clear) {
            mDataBinding.etNumber3.setText("");
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm(String text);
    }
}
